package cn.test.livedatademo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
//    companion object {
        val nameLiveData: MutableLiveData<String> = MutableLiveData()
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //默认加载第一个Fragment
        replaceFragment(FirstFragment(), R.id.containerLayout)
        //对page1Tv控件添加点击事件，点击后切换至FirstFragment
        page1Tv.setOnClickListener { replaceFragment(FirstFragment(), R.id.containerLayout) }
        //对page2Tv控件添加点击事件，点击后切换至SecondFragment
        page2Tv.setOnClickListener { replaceFragment(SecondFragment(), R.id.containerLayout) }
    }

    private fun replaceFragment(fragment: Fragment, containerId: Int) =
        supportFragmentManager.beginTransaction().replace(containerId, fragment).commit()
}